package com.heima.wemedia.controller.v1;

import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SaveSensitiveDto;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.service.WmSensitiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-09-11 14:28
 */
@RestController
@RequestMapping("/api/v1/sensitive")
@Api(value = "敏感词接口文档",tags = "敏感词管理")
public class SensitiveController {

    @Autowired
    private WmSensitiveService wmSensitiveService;

    @GetMapping("/test")
    public String getId(HttpServletRequest request){
        return WmThreadLocalUtil.getUser().getId().toString();
    }

    //1.save?sensitive=测试 查询参数 String sensitive
    //2.路径参数 /save/1

    /**
     * @PostMapping("/save/{sensitive}")
     * public ResponseResult save(@PathVariable("sensitive") String sensitive)
     */

    //3.json数据对象传递 @RequestBody
    @PostMapping("/save")
    @ApiOperation("保存敏感词")
    public ResponseResult save(@RequestBody SaveSensitiveDto sensitive){
        String sensitives = sensitive.getSensitives();
        //1.校验参数
        if(StringUtils.isBlank(sensitives)){
            //return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.service处理
        wmSensitiveService.saveSensitive(sensitives);

        //3.返回成功结果
        return ResponseResult.okResult(null);
    }

    @PostMapping("/update")
    @ApiOperation("更新敏感词")
    public ResponseResult update(@RequestBody SaveSensitiveDto sensitive){
        String sensitives = sensitive.getSensitives();
        //1.校验参数
        if(sensitive.getId() == null || StringUtils.isBlank(sensitives)){
            //return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.service处理
        wmSensitiveService.updateSensitive(sensitive);

        //3.返回成功结果
        return ResponseResult.okResult(null);
    }


    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id){

        return wmSensitiveService.removeById(id)?ResponseResult.okResult(null):ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    @PostMapping("/list")
    public ResponseResult list(@RequestBody SensitiveDto sensitiveDto){
        //1.检查参数
        sensitiveDto.checkParam(); //将page size重置

        //2.查询
        PageResponseResult page = wmSensitiveService.findPage(sensitiveDto);

        return page;
    }

    public static void main(String[] args) {
        String s = UUID.randomUUID().toString().replace("-","");
        System.out.println("salt:" + s);
        String s1 = DigestUtils.md5DigestAsHex(("123456" + s).getBytes(StandardCharsets.UTF_8));
        System.out.println("加密后的密码:" + s1);
        String name = Thread.currentThread().getName();
        System.out.println(name);
    }
}
